﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Project.Models;
namespace Project.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Upload()
        {
            return View();
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            String ftpServerIP = "216.3.4.67";
            String ftpUserID = "sunset";
            String ftpPassword = "fu5aFEqa";
            String filename = file.FileName;

            string destinationPath = @"/UploadFiles/";
            FileInfo fileInf = new FileInfo(Path.GetFileName(file.FileName));
            FileInfo fileInf1 = new FileInfo(file.FileName);

             
            FileUpload chkFile = new FileUpload();
           
                chkFile.DeleteFile(filename);


           
                try
                {

                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerIP + destinationPath + Path.GetFileName(file.FileName));
                    request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    request.Method = WebRequestMethods.Ftp.UploadFile;

                    request.UseBinary = false;
                    request.UsePassive = false;

                    request.ContentLength = file.ContentLength;
                    // The buffer size is set to 2kb
                    int buffLength = 2048668;
                    byte[] buff = new byte[buffLength];
                    int contentLen;
                    // Opens a file stream (System.IO.FileStream) to read the file to be uploaded


                    FileStream fs = fileInf1.OpenRead();
                    try
                    {
                        // Stream to which the file to be upload is written
                        Stream strm = request.GetRequestStream();
                        // Read from the file stream 2kb at a time
                        contentLen = fs.Read(buff, 0, buffLength);
                        // Until Stream content ends
                        while (contentLen != 0)
                        {
                            // Write Content from the file stream to the FTP Upload Stream
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);
                        }
                        // Close the file stream and the Request Stream
                        strm.Close();
                        fs.Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message, "Upload Error");
                    }


                }
                catch (Exception Ex)
                {
                    Console.WriteLine(Ex.ToString());
                    Console.ReadKey(false);
                }
            
                
            //}
            //else
            //{ }

            return View();

        }


        public ActionResult DeleteFile(string ftpURL, string UserName, string Password, string ftpDirectory, string FileName)
        {

            string _ftpURL = "testftp.com";     //Host URL or address of the FTP server
            string _UserName = "admin";         //User Name of the FTP server
            string _Password = "admin123";      //Password of the FTP server
            string _ftpDirectory = "Receipts";  //The directory in FTP server where the files will be uploaded
            string _FileName = "test1.csv";     //File name, which one will be uploaded
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL + "/" + ftpDirectory + "/" + FileName);
            ftpRequest.Credentials = new NetworkCredential(UserName, Password);
            ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
            FtpWebResponse responseFileDelete = (FtpWebResponse)ftpRequest.GetResponse();

            return View();
        }

    }
}